using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SharpWind.Graphics.Settings;

namespace SharpWind.Framework
{
	public partial class SettingsForm : Form
	{
		private bool windowed = true;

		private bool vsync = false;

		private int bitsPerPixel = 0;

		private int width = 0;

		private int height = 0;

		private int refreshRate = 0;

		public SettingsForm()
		{
			InitializeComponent();
		}

		private void cancelButton_Click( object sender, EventArgs e )
		{
			DialogResult = DialogResult.Cancel;
		}

		private void saveButton_Click( object sender, EventArgs e )
		{
			/*Config config = new Config();
			config.Add( "Windowed", this.windowed.ToString() );

			config.Add( "Width", this.width.ToString() );
			config.Add( "Height", this.height.ToString() );
			config.Add( "BitsPerPixel", this.bitsPerPixel.ToString() );
			config.Add( "V-Sync", this.vsync.ToString() );
			config.Add( "RefreshRate", this.refreshRate.ToString() );

			config.Save( Config.Settings );*/

			DialogResult = DialogResult.OK;
		}

		private void UpdateMode()
		{
			if( this.windowed )
			{
				this.vsyncCheckBox.Checked = false;
				this.vsyncCheckBox.Enabled = false;

				this.colorDepthComboBox.Items.Clear();
				this.colorDepthComboBox.Enabled = false;

				this.refreshRateComboBox.Items.Clear();
				this.refreshRateComboBox.Enabled = false;
			}
			else
			{
				this.vsyncCheckBox.Enabled = true;

				this.colorDepthComboBox.Items.Clear();
				this.colorDepthComboBox.Enabled = true;

				this.resolutionComboBox.Items.Clear();
				this.resolutionComboBox.Enabled = true;

				this.refreshRateComboBox.Items.Clear();
				this.refreshRateComboBox.Enabled = true;
			}

			foreach( DisplayMode displayMode in Engine.Instance.SupportedDisplayModes )
			{
				if( this.colorDepthComboBox.Items.IndexOf( displayMode.BitsPerPixel ) == -1 )
				{
					this.colorDepthComboBox.Items.Add( displayMode.BitsPerPixel );
				}
			}

			this.colorDepthComboBox.SelectedIndex = 0;
		}

		private void UpdateResolution()
		{
			foreach( DisplayMode displayMode in Engine.Instance.SupportedDisplayModes )
			{
				if( displayMode.BitsPerPixel == this.bitsPerPixel )
				{
					if( this.resolutionComboBox.Items.IndexOf( displayMode.ToString() ) == -1 )
					{
						this.resolutionComboBox.Items.Add( displayMode.ToString() );
					}
				}
			}

			this.resolutionComboBox.SelectedIndex = 0;
		}

		private void UpdateRefresh()
		{
			foreach( DisplayMode displayMode in Engine.Instance.SupportedDisplayModes )
			{
				if( displayMode.ToString() == this.resolutionComboBox.SelectedItem.ToString() )
				{
					if( this.refreshRateComboBox.Items.IndexOf( displayMode.Mode.RefreshRate ) == -1 )
					{
						this.refreshRateComboBox.Items.Add( displayMode.Mode.RefreshRate );
					}
				}
			}

			this.refreshRateComboBox.SelectedIndex = 0;
		}

		private void SettingsForm_Load( object sender, EventArgs e )
		{
			this.displayAdapterTextBox.Text = Engine.Instance.GraphicsAdapterDetails.Description;
			this.driverVersionTextBox.Text  = Engine.Instance.GraphicsAdapterDetails.DriverVersion.ToString();
			UpdateMode();
		}

		private void fullscreenRadioButton_CheckedChanged( object sender, EventArgs e )
		{
			if( this.fullscreenRadioButton.Checked )
			{
				this.windowed = false;
				UpdateMode();
			}
		}

		private void windowedRadioButton_CheckedChanged( object sender, EventArgs e )
		{
			if( this.windowedRadioButton.Checked )
			{
				this.windowed = true;
				UpdateMode();
			}
		}

		private void colorDepthComboBox_SelectedIndexChanged( object sender, EventArgs e )
		{
			if( this.colorDepthComboBox.SelectedItem != null )
			{
				this.bitsPerPixel = (int)this.colorDepthComboBox.SelectedItem;
				UpdateResolution();
			}
		}

		private void resolutionComboBox_SelectedIndexChanged( object sender, EventArgs e )
		{
			if( this.resolutionComboBox.SelectedItem != null )
			{
				string[] resolution = ( (string)this.resolutionComboBox.SelectedItem ).Split( 'x' );
				if( resolution.Length == 2 )
				{
					this.width  = int.Parse( resolution[0] );
					this.height = int.Parse( resolution[1] );
				}
				UpdateRefresh();
			}
		}

		private void refreshRateComboBox_SelectedIndexChanged( object sender, EventArgs e )
		{
			if( this.refreshRateComboBox.SelectedItem != null )
			{
				this.refreshRate = (int)this.refreshRateComboBox.SelectedItem;
			}
		}
	}
}